<?php
namespace Adminx\Controller;

class ShouzhiController extends AdminController {

	#列表
	public function index() {
		$type = I('type');  
		$keyword = I('keyword');  
        $startDate = I('startDate');    
        $endDate = I('endDate');	

        if ($keyword!='') {
            $map['title'] = array('like','%'.$keyword.'%');
        }

        if ($type!='') {
            $map['type'] = $type;
        }        

        if ($startDate!='' && $endDate!='') {
            $map['actionDate'] = array('between',strtotime($startDate).','.(strtotime($endDate)+86399));
        } 

		$obj = M('Shouzhi');
        $count = $obj->where($map)->count();

        $zhichu = $obj->where('type=2')->sum('money');
        $shouru = $obj->where('type=1')->sum('money');
        $total = $shouru-$zhichu;
        import("Common.ORG.Page");
        $page = new \Page($count, 15);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('zhichu', $zhichu);
        $this->assign('shouru', $shouru);
        $this->assign('total', $total);
        $this->display();
	}

	#添加
	public function add() {
		if($_POST){
	        $obj = D('Shouzhi');
	        if ($data = $obj->create()) {
	        	$data['doID'] = $this->admin['id'];
	        	$data['doName'] = $this->admin['name'];
	            if ($list = $obj->add($data)) {
	                $this->success('操作成功',$back);
	            } else {
	                $this->error('操作失败');
	            }
	        } else {
	            $this->error($obj->getError());
	        }    		
    	}else{
    		$type = I('get.type');
    		switch ($type) {
    			case '1':
    				$typeName = '收入';
    				break;
    			case '2':
    				$typeName = '支出';
    				break;
    			default:
    				$this->error('参数错误');
    				break;
    		}
    		$this->assign('type',$type);
    		$this->assign('typeName',$typeName);
			$this->display();
		}
	}

	#编辑
	public function edit() {
		if($_POST){
    		$this->all_save('Shouzhi',U('Shouzhi/index'));
    	}else{
			$id = (int) $_GET['id'];
			if (!isset ($id)) {
				$this->error('参数错误');
			}
			$obj = M('Shouzhi');
			$map['id']=$id;
			$list = $obj->where($map)->find();
			if (!$list) {
				$this->error('信息不存在');
			} else {
	    		switch ($list['type']) {
	    			case '1':
	    				$typeName = '收入';
	    				break;
	    			case '2':
	    				$typeName = '支出';
	    				break;
	    			default:
	    				$this->error('参数错误');
	    				break;
	    		}
				$this->assign('list', $list);
				$this->assign('typeName', $typeName);
				$this->display();
			}
		}		
	}


	#删除
	public function del() {
		$this->all_del('Shouzhi','reload');
	}
}
?>